﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySqlCodeGeneratorLib;

namespace DBCodeGenerator
{
    public partial class frmmysqlLogin : Form
    {
        UserSettings usettings;
        public frmmysqlLogin()
        {
            InitializeComponent();
            usettings = new UserSettings();
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            this.CallAuthProcess();
        }

        private void CallAuthProcess()
        {
            if (Authenticate())
            {
                usettings.SaveSettings(cmbservers.Text);
                this.ShowInTaskbar = false;
                this.Visible = false;
                frmcsharpsettings set = new frmcsharpsettings();
                set.ShowDialog();
                this.Close();
            }
        }

        private dbtype GetSelectedType()
        {
            if (cmbservers.Text == "")
                return dbtype.MSSQL_SERVER_2008R2;
            else if (cmbservers.Text == "")
                return dbtype.MYSQL_SERVER;
            else
                return dbtype.NONE;
        }


        private bool Authenticate()
        {
            bool Status = false;
            try
            {
                if (String.IsNullOrEmpty(txtlogin.Text))
                {
                    MessageBox.Show("Invalid user name!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                else if (String.IsNullOrEmpty(txtpwd.Text))
                {
                    MessageBox.Show("Invalid password!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                else if (String.IsNullOrEmpty(txtport.Text))
                {
                    MessageBox.Show("Invalid port number !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                else if (String.IsNullOrEmpty(cmbservers.Text))
                {
                    MessageBox.Show("Invalid server name!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }

                MySqlInformation mysqlinfor = new MySqlInformation();
                if (mysqlinfor.Login(cmbservers.Text, txtlogin.Text, txtpwd.Text, txtport.Text) != null)
                    Status = true;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Status = false;
            }
            

            if (Status)
            {
                GlobalSettings.Uname = txtlogin.Text.Trim();
                GlobalSettings.Password = txtpwd.Text.Trim();
                GlobalSettings.ServerName = cmbservers.Text.Trim();
                GlobalSettings.Port = txtport.Text.Trim();
            }

            return Status;
        }
    }
}
